﻿using Microsoft.Data.Sqlite;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BingWallpaper.DAL
{
    public class DBContext
    {
        protected static SqlSugarClient _db;

        public static SqlSugarClient GetDbContext()
        {

            if (_db != null)
            {
                return _db;
            }

            string dbName = Path.Combine(Environment.CurrentDirectory, "BingWallpaper.db");
            string connStr = new SqliteConnectionStringBuilder()
            {
                DataSource = dbName,
                Mode = SqliteOpenMode.ReadWriteCreate,
                // Password = "123456"
            }.ToString();


            //创建数据库对象 SqlSugarClient   
            _db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = connStr,//连接符字串
                DbType = DbType.Sqlite, //数据库类型
                IsAutoCloseConnection = true //不设成true要手动close
            });

            if (!_db.DbMaintenance.IsAnyTable(nameof(Models.Entitys.BingImagesModel)))
            {
                _db.CodeFirst.InitTables<Models.Entitys.BingImagesModel>();
            }
          

            return _db;

        }




    }
}
